<template>
    <el-dialog ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="40%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-row :gutter="15">
                <el-col :span="12">
                <el-form-item label="疾病名称" prop="diseaseName">
                    <el-input v-model="ruleForm.diseaseName" clearable placeholder="请输入疾病名称" />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="关联品种" prop="fishVarietyId">
                    <el-select class="w100p" v-model="ruleForm.fishVarietyId" filterable placeholder="请选择养殖品种"
                        @change="handleChangePlantVariety">
                        <el-option v-for="item, index in varietyList" :key="index" :label="item.varietyName"
                            :value="item.fishVarietyId"></el-option>
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="诊断项症状" prop="diseaseManegeList">
                    <el-select class="w100p" filterable clearable v-model="ruleForm.diseaseManegeList"
                        placeholder="请选择诊断项症状" multiple>
                        <el-option v-for="item of diagnosisItemSymptomList" :key="item.diagnosisItemSymbolNo"
                            :label="`症状名称：${item.diagnosisItemName}（${item.diagnosisItemSymbolNo}）：${item.diagnosisItemSymbolDesc}`"
                            :value="item.diagnosisItemSymbolNo" />
                    </el-select>
                </el-form-item>
            </el-col>
            </el-row>
            <el-form-item label="特殊症状描述">
                <el-input v-model="ruleForm.mainSymptoms" type="textarea" placeholder="请输入特殊症状描述" clearable :rows="2" />
            </el-form-item>

            <el-form-item label="预防方法" prop="preventionMethod">
                <el-input v-model="ruleForm.preventionMethod" type="textarea" placeholder="请输入预防方法" clearable
                    :rows="2" />
            </el-form-item>

            <el-form-item label="治疗方法" prop="therapeuticMethod">
                <el-input v-model="ruleForm.therapeuticMethod" type="textarea" placeholder="请输入治疗方法" clearable
                    :rows="2" />
            </el-form-item>

            <!-- 这里添加诊断项症状编号 -->
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <SaveDraftBox ref="saveDraft" v-if="addEditFlag == 'add' && dialogVisible" :formData="ruleForm"
                @pushData="pushData">
            </SaveDraftBox>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import diseaseManagement from '@/api/disease-prevention-and-control/characteristic-management.js';
export default {
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        varietyList: {
            type: Array,
            default: () => {
                return []
            }
        },
        diagnosisItemSymptomList: {
            type: Array,
            default: () => {
                return []
            }
        },
        diagnosisItemList: {
            type: Array,
            default: () => {
                return []
            }
        }
    },
    data() {
        return {
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            rules: {
                diseaseName: [
                    {
                        required: true,
                        message: "请输入疾病名称",
                        trigger: ["change", "blur"]
                    }
                ],
                mainSymptoms: [
                    {
                        required: true,
                        message: "请输入疾病症状描述",
                        trigger: ["change", "blur"]
                    }
                ],
                fishVarietyId: [
                    {
                        required: true,
                        message: "请选择品种",
                        trigger: ["change", "blur"]
                    }
                ],
                diseaseManegeList: [
                    {
                        required: true,
                        message: "请选择诊断项症状",
                        trigger: ["change", "blur"]
                    }
                ]
            },
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.ruleForm = {}
                this.$set(this.ruleForm, 'diseaseManegeList', [])
                this.addEditApi = diseaseManagement.add
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                let dml = this.data.diagnosisItemSymbolNoList ? this.data.diagnosisItemSymbolNoList.split(',') : []
                this.$set(this.ruleForm, 'diseaseManegeList', dml)
                this.addEditApi = diseaseManagement.edit
                this.addEditName = '编辑'
            }
        },

        pushData(data) {
            this.ruleForm = { ...data };
            this.$nextTick(() => {
                this.$refs.ruleForm.clearValidate();
            })
        },
        handleChangePlantVariety() {
            let varietyName = this.$public.foundObject(this.varietyList, 'fishVarietyId', this.ruleForm.fishVarietyId).varietyName || '';
            this.$set(this.ruleForm, 'varietyName', varietyName)
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({
                        ...this.ruleForm,
                        diagnosisItemSymbolNoList: this.ruleForm.diseaseManegeList.join(",")
                    }).then(res => {
                        let msg = res.success ? '操作成功' : '操作失败'
                        this.$message[res.success ? 'success' : 'warning'](res.message || msg)
                        res.success && this.cancel();
                        //提交数据时删除保存的记录
                        res.success && this.addEditFlag == 'add' && this.$refs.saveDraft.delByKey();
                    }).catch(err => {
                        this.$message.error(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>

<style lang="scss" scoped>
.line {
    border-bottom: 1px solid #e6e6e6;
}

.icon {
    font-size: 20px;
    line-height: 29px;
    cursor: pointer;
}

::v-deep .noLabel {
    .el-form-item {
        .label {
            display: none;
        }

        .el-form-item__content {
            margin: 0;
        }
    }
}
</style>